package myJs

import myJs.Utils._
import myJs.myPkg.jquery._
import org.scalajs.dom.ext.Ajax

import scala.scalajs.js.annotation.{JSExport, JSExportTopLevel}
import myJs.Implicits._
import myJs.tool.Tool
import myJs.tool.Tool._

import scala.scalajs.js.Date
import scala.scalajs.js
import scala.scalajs.js.Dynamic.{global => g}
import org.scalajs.macrotaskexecutor.MacrotaskExecutor.Implicits._
/**
 * Created by yz on 2019/4/25
 */
@JSExportTopLevel("Header")
object Header {

  @JSExport("init")
  def init = {
    refreshCoin
    checkHasSignIn
  }

  @JSExport("selectHeader")
  def selectHeader(id: String) = {
    $(".page-actions li").removeClass("current")
    $(s".page-actions #${id}").addClass("current")
  }

  def refreshCoin = {
    val url = g.jsRoutes.controllers.UserController.getCoin().url.toString
    Ajax.get(url = s"${url.noCache}", headers = Map("Content-Type" -> "application/json")).map { xhr =>
      val coin = xhr.responseText
      $(".coin").text(coin)
    }
  }

  def checkHasSignIn = {
    val myDate = new Date()
    val url = g.jsRoutes.controllers.UserController.getHasSignInCurMonth().url.toString
    Ajax.get(url = s"${url.noCache}", headers = Map("Content-Type" -> "application/json")).map { xhr =>
      val dateArray = xhr.responseText.toJArI
      if (dateArray.contains(myDate.getDate().toInt)) {
        $("#hasSignIn").show()
        $("#noSignIn").hide()
      } else {
        $("#hasSignIn").hide()
        $("#noSignIn").show()
      }
    }
  }

}
